<?php

    require_once Core_IO_Path::combine(SERUM_LIB_PATH, 'SimplePie', 'simplepie.inc.php');
    require_once Core_IO_Path::combine(SERUM_LIB_PATH, 'SimplePie', 'idn', 'idna_convert.class.php');

    /**
     * RSS parser
     *
     * @package      Core
     * @subpackage   Parser
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
	class Core_Parser_RSS
	{

	////////////////////////////////////////////////////////////////////////////
	//                               Attributes                               //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Cache data
         *
         * @var     array
         */
        protected $_data = array();

        /**
         * Parser instance
         *
         * @var     SimplePie
         */
        protected $_parser = null;

	////////////////////////////////////////////////////////////////////////////
	//                              Getters / Setters                         //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Gets items
         *
         * @param       int         $start
         * @param       int         $end
         * @return      array
         */
        public function getItems($start = 0, $end = 0)
        {
            if ($this->_parser === null) {
                throw new Core_Exception('Fatal error : parser must be initialized before getting data');
            }

            return $this->_parser->get_items($start, $end);
        }

        /**
         * Gets description
         *
         * @return      array
         */
        public function getDescription()
        {
            if ($this->_parser === null) {
                throw new Core_Exception('Fatal error : parser must be initialized before getting data');
            }

            return $this->_parser->get_description();
        }

        /**
         * Gets title
         *
         * @return      array
         */
        public function getTitle()
        {
            if ($this->_parser === null) {
                throw new Core_Exception('Fatal error : parser must be initialized before getting data');
            }

            return $this->_parser->get_title();
        }

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

		/**
		 * Constructor
         *
         * @param       boolean     $cache
         * @param       integer     $cacheDuration
		 */
		public function __construct($cache = true, $cacheDuration = 3600)
		{
            $this->_parser = new SimplePie();

            if ($cache === true) {
                $path = Core_IO_Path::combine(WWW_PATH, 'temp', 'compiled', 'rss');

                if (!Core_IO_Directory::exists($path)) {
                    Core_IO_Directory::createDirectory($path);
                }

                $this->_parser->set_cache_location($path);
                $this->_parser->set_cache_duration($cacheDuration);
            }
		}

		/**
		 * Destructor
		 */
		public function __destruct()
		{
			unset($this);
		}

		/**
		 * Parses an URL
		 *
		 * @param		string		$url
		 * @throws		Core_ArgumentException
		 * @return		array
		 */
		public function read($url)
		{
            $this->_parser->set_feed_url($url);

            if (!$this->_parser->init()) {
                throw new Core_Exception('Fatal error : unable to parse feed '. $url);
            }

            return $this;
        }
	}
